<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>晴天医诊</title>
    <meta name="description" content="晴天医诊">
    <meta name="keywords" content="晴天医诊">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>角色管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>角色管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="角色名称" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="id" label="用户编号" align="center"></el-table-column>
                <el-table-column prop="name" label="用户姓名" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
<!--
                        <el-button type="primary" size="mini" @click="allocationMenu(scope.row)">分配菜单</el-button>
-->
                        <!--                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>-->
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增角色权限" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="名称">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="关键字">
                                                <el-input v-model="formData.keyword"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="描述">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="分配菜单" name="second">
                                <el-form ref="dataAddForm" :model="formData" label-position="right" label-width="100px">

                                    <el-tree ref="tree"
                                             :data="menuData"
                                             node-key="id"
                                             :show-checkbox="true"
                                             :default-checked-keys="defaultCheckedKeys"
                                             default-expand-all
                                             :expand-on-click-node="false">
                                    </el-tree>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="权限信息" name="three">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>权限id</th>
                                            <th>权限名称</th>
                                            <th>权限关键词</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="permissionIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.id}}</label></td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑角色权限" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="名称">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="关键字">
                                                <el-input v-model="formData.keyword"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="描述">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="分配菜单" name="second">
                                <el-form ref="dataAddForm" :model="formData" label-position="right" label-width="100px">

                                    <el-tree ref="tree"
                                             :data="menuData"
                                             node-key="id"
                                             :show-checkbox="true"
                                             :default-checked-keys="defaultCheckedKeys"
                                             default-expand-all
                                             :expand-on-click-node="false">
                                    </el-tree>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="权限信息" name="three">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>权限id</th>
                                            <th>权限名称</th>
                                            <th>权限关键词</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="permissionIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.id}}</label></td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            tableData: [],
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            permissionIds: [],////新增和编辑表单中权限项对应的复选框，基于双向绑定可以进行回显和数据提交
            menuData: [],
            currentRole: '',
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            menuIds: [],
            defaultCheckedKeys: [],//tree默认选中数据
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        mounted() {
        },
        methods: {
            //编辑
            handleEdit() {
                //我们可以把用户勾选的检查项id集合和检查组的基本信息打包一起提交
                this.formData.permissionIds = this.permissionIds;
                axios.post("/role/edit.do", this.formData).then((res) => {
                    //关闭弹框，刷新页面，
                    if (res.data.flag) {

                        //获取树形菜单选中的值
                        let menus = this.$refs['tree'].getCheckedKeys();
                        let halfCheckedKeys = this.$refs['tree'].getHalfCheckedKeys();//如果子节点没有全部选中返回的是父节点id
                        for (var i = 0; i < halfCheckedKeys.length; i++) {
                            menus.push(halfCheckedKeys[i])
                        }
                        console.log(this.currentRole);
                        console.log(menus);
                        axios.post("/role/addRoleMenu.do?roleId=" + this.currentRole + "&menus=" + menus).then((res) => {
                            this.dialogFormVisible4Edit = false;
                            this.findPage();
                        });
                    }
                    //不管成功失败都要提示用户，只是提示的样式不一样
                    this.$message({
                        message: res.data.flag ? "编辑成功" : "编辑失败",
                        type: res.data.flag ? 'success' : "error"
                    });
                });
            },
            //添加
            handleAdd() {
                //权限的基本信息在formData里面
                //用户勾选的权限项id集合 permissionIds
                //我们最好把id集合和formData打包一起传
                this.formData.permissionIds = this.permissionIds;
                //在后端接收参数的pojo里面加上一个属性 List<Integer> permissionIds
                axios.post("/role/add.do", this.formData).then((res) => {
                    if (res.data.flag) {
                        //成功关闭弹框 、 刷新页面
                        this.dialogFormVisible = false;
                        //this.findPage();
                    }
                });

                //获取树形菜单选中的值
                let menus = this.$refs['tree'].getCheckedKeys();
                let halfCheckedKeys = this.$refs['tree'].getHalfCheckedKeys();//如果子节点没有全部选中返回的是父节点id
                for (var i = 0; i < halfCheckedKeys.length; i++) {
                    menus.push(halfCheckedKeys[i])
                }

                axios.post("/role/addRoleMenu.do?roleId=" + this.currentRole + "&menus=" + menus).then((res) => {
                    this.dialogFormVisible = false;
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? "success" : "error"
                    })
                });
            },
            //分页查询
            findPage() {
                axios.post("/role/findPage.do", this.pagination).then((res) => {
                    this.dataList = res.data.data.rows;
                    this.pagination.total = res.data.data.total;
                })
            },
            // 重置表单
            resetForm() {
                this.formData = {}
            },
            // 弹出添加窗口
            handleCreate() {
                //this.dialogFormVisible = true;
                //查询所有权限
                axios.get("/permission/findAll.do").then((res) => {
                    console.log(res.data);
                    //res.data = {flag,message,data}
                    this.tableData = res.data.data;
                });
                axios.get("/menu/getMenus.do?id=0&roleId=0").then((res) => {
                    this.menuData = res.data.data.menus;//所有的菜单
                    //弹出新增框
                    this.dialogFormVisible = true;
                });
            },
            //分配菜单
            allocationMenu(row) {
                axios.get("/menu/getMenus.do?id=0&roleId=" + row.id).then((res) => {
                    this.menuData = res.data.data.menus;//所有的菜单
                    this.currentRole = row.id;
                    //弹出编辑框
                    this.dialogFormVisible4Edit = true;
                    // this.menuIds =
                    this.defaultCheckedKeys = res.data.data.menuIds;//用户拥有的菜单id
                })
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                //回显三个数据需要发送3个ajax，这样太浪费时间了，我们只发一个请求返回三个数据
                //  axios.get("/checkgroup/findById4Edit.d?id=" + row.id)
                axios.post("/role/findById4Edit.do", {id: row.id}).then((res) => {
                    //res.data axios框架的data属性
                    //res.data.data 是我们result对象里面的data属性
                    console.log(res.data.data);

                    this.permissionIds = res.data.data.permissionIds;//给checkitemIds
                    //给tableData
                    this.tableData = res.data.data.permissionss;
                    //给formData
                    this.formData = res.data.data.role;
                    this.allocationMenu(row);
                })

            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 删除
            handleDelete(row) {//row 当前行的数据
                //先要询问是否删除
                this.$confirm('是否删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //点击确定以后的操作  向后台传输数据
                    axios.delete("/role/delete.do?id=" + row.id).then((res) => {
                        if (res.data.flag) {
                            this.findPage()
                        }

                        //不管成功失败都要提示用户，只是提示的样式不一样
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? 'success' : "error"
                        });

                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }

        }
    })
</script>
</html>
